home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / Akua Sweets 131 / Akua Sweets Examples / File / Concat < prev    next >
Encoding:
Text File  |  1999-03-04  |  3.5 KB  |  126 lines  |  [TEXT/ToyS]

  1. property sasDoFolders : true
  2. property sasOutExtension : ".cat" -- Added to file name of first file for output file (can't be empty string)
  3. property sasFoldExtension : " ƒ" -- Added to folder name when folder is concated (can be empty string)
  4.  
  5. property sasFilesProgLoc : {0, 0}
  6.  
  7.  
  8. on open fsObjs
  9.     set fileList to {}
  10.     
  11.     repeat with fsObj in fsObjs
  12.         set fInfo to basic info for fsObj
  13.         
  14.         if (catalog kind of fInfo) is a folder then
  15.             if (sasDoFolders) then
  16.                 ConcatFolder(fsObj)
  17.             else
  18.                 beep
  19.             end if
  20.         else
  21.             set fileList to fileList & fsObj
  22.         end if
  23.     end repeat
  24.     
  25.     if (count (fileList)) > 1 then ¬
  26.         |concat|(fileList, item 1 of fileList)
  27. end open
  28.  
  29.  
  30.  
  31. on ConcatFolder(fsObj)
  32.     set catFold to (fsObj as string)
  33.     set fileList to {}
  34.     
  35.     set fileNames to list folder fsObj
  36.     
  37.     repeat with fname in fileNames
  38.         (* Only add files; ignore deeper levels
  39.             If you want to concat deeper folders into separate files, then just ConcatFolder(newItem)
  40.             If you want to concat them to the main file, you'll need to do a bit more…
  41.         *)
  42.         set newItem to (catFold & fname) as alias
  43.         if (catalog kind of (basic info for newItem) is a file) then ¬
  44.             set fileList to fileList & {newItem}
  45.     end repeat
  46.     
  47.     if (count (fileList)) > 1 then ¬
  48.         |concat|(fileList, fsObj)
  49. end ConcatFolder
  50.  
  51.  
  52. on |concat|(fsObjs, fsFirstSrc)
  53.     set idx to 0
  54.     set cnt to the number of items in fsObjs
  55.     
  56.     -- File Progress Window
  57.     set pgFiles to display progress titled "Concatenating" subtitled ¬
  58.         "Setting up…" maximum cnt located at sasFilesProgLoc
  59.     
  60.     -- Open new file based on first source file/folder
  61.     set fsInfo to extended info for fsFirstSrc
  62.     
  63.     if (the catalog kind of fsInfo is a file) then
  64.         -- Create output in same folder as file
  65.         set outRef to open fork from fsFirstSrc ¬
  66.             extended by sasOutExtension with write access
  67.     else
  68.         -- Create output of same type as first file we use…
  69.         set fsItemInfo to basic info for (item 1 of fsObjs)
  70.         set firstType to system type of fsItemInfo
  71.         set firstCreator to system creator of fsItemInfo
  72.         
  73.         -- Create output at same level as folder (in its parent)
  74.         set outRef to open fork from (parent spec of fsInfo) ¬
  75.             named (catalog name of fsInfo) & sasFoldExtension & sasOutExtension ¬
  76.             of type firstType of creator firstCreator ¬
  77.             with write access
  78.     end if
  79.     
  80.     -- Concat the files
  81.     repeat with fsObj in fsObjs
  82.         set fInfo to basic info for fsObj
  83.         set fLen to data fork length of fInfo
  84.         
  85.         if (fLen is not 0) then
  86.             set fsName to catalog name of fInfo
  87.             
  88.             display progress pgFiles subtitled fsName
  89.             set oneRef to open fork from fsObj
  90.             
  91.             set pgOne to display progress titled fsName maximum fLen
  92.             set maxLen to fLen
  93.             
  94.             repeat while (fLen > 0)
  95.                 -- Do a 64K chunk (or whatever is left)
  96.                 set bufLen to 64 * 1024
  97.                 if (bufLen > fLen) then set bufLen to fLen
  98.                 set fLen to fLen - bufLen
  99.                 
  100.                 -- Read a chunk                
  101.                 display progress pgOne labeled "Reading…"
  102.                 set buf to read data from oneRef using buffer size bufLen
  103.                 
  104.                 -- Write the chunk
  105.                 display progress pgOne labeled "Writing…" value (maxLen - fLen) with alternate color
  106.                 write data to outRef given «class Data»:buf
  107.                 
  108.                 display progress pgOne value (maxLen - fLen) without alternate color
  109.             end repeat
  110.             
  111.             display progress pgOne with disposal
  112.             close fork oneRef
  113.         end if
  114.         
  115.         -- Save location of progress window
  116.         set idx to idx + 1
  117.         set sasFilesProgLoc to screen location of (display progress pgFiles value idx)
  118.     end repeat
  119.     
  120.     -- Close file
  121.     close fork outRef
  122.     
  123.     -- Kill progress window
  124.     display progress pgFiles with disposal
  125. end |concat|
  126.